package org.example.examsystem.controller;

import org.example.examsystem.entity.User;
import org.example.examsystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public Map<String, Object> login(@RequestBody Map<String, String> loginInfo) {
        String username = loginInfo.get("username");
        String password = loginInfo.get("password");
        String role = loginInfo.get("role");

        User user = userService.findUserByUsernameAndPassword(username, password);

        Map<String, Object> result = new HashMap<>();
        if (user != null) {
            int roleId = user.getRoleid();
            String expectedRole = "";
            switch (roleId) {
                case 1:
                    expectedRole = "teacher";
                    break;
                case 2:
                    expectedRole = "student";
                    break;
                case 3:
                    expectedRole = "admin";
                    break;
                default:
                    result.put("success", false);
                    result.put("message", "未知角色ID");
                    return result;
            }

            if (!role.equals(expectedRole)) {
                result.put("success", false);
                result.put("message", "选择的角色与账号不符");
                return result;
            }

            String redirectUrl = "";
            switch (role) {
                case "admin":
                    redirectUrl = "/admin";
                    break;
                case "teacher":
                    redirectUrl = "/teacher";
                    break;
                case "student":
                    redirectUrl = "/student";
                    break;
                default:
                    result.put("success", false);
                    result.put("message", "无效的角色");
                    return result;
            }
            result.put("success", true);
            result.put("redirectUrl", redirectUrl);
            result.put("truename", user.getTruename());
            result.put("userid", user.getUserid()); // 返回用户的userid
            result.put("classid", user.getClassid()); // 返回用户的userid
        } else {
            result.put("success", false);
            result.put("message", "用户名或密码错误");
        }
        return result;
    }

    @PostMapping("/user/register")
    public String registerUser(@RequestBody User user) {
        System.out.println("Received user registration request: " + user);
        int result = userService.registerUser(user);
        if (result > 0) {
            return "注册成功";
        } else {
            return "注册失败";
        }
    }

    @PostMapping("/user/changePassword")
    public Map<String, Object> changePassword(@RequestBody Map<String, String> passwordInfo) {
        String userId = passwordInfo.get("userId");
        String oldPassword = passwordInfo.get("oldPassword");
        String newPassword = passwordInfo.get("newPassword");

        Map<String, Object> result = new HashMap<>();
        User user = userService.findUserById(Integer.parseInt(userId));
        if (user == null) {
            result.put("success", false);
            result.put("message", "用户不存在");
            return result;
        }
        if (!user.getUserpwd().equals(oldPassword)) {
            result.put("success", false);
            result.put("message", "原密码错误");
            return result;
        }
        int updateResult = userService.updatePassword(Integer.parseInt(userId), newPassword);
        if (updateResult > 0) {
            result.put("success", true);
            result.put("message", "密码修改成功");
        } else {
            result.put("success", false);
            result.put("message", "密码修改失败");
        }
        return result;
    }
}